package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Cleaning;
import com.ruoyi.system.service.ICleaningService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务类型Controller
 *
 * @author ruoyi
 * @date 2021-06-18
 */
@RestController
@RequestMapping("/system/clean")
public class CleaningController extends BaseController {
    @Autowired
    private ICleaningService cleaningService;

    /**
     * 查询服务类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:cleaning:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cleaning cleaning) {
        startPage();
        List<Cleaning> list = cleaningService.selectCleaningList(cleaning);
        return getDataTable(list);
    }

    /**
     * 导出服务类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:cleaning:export')")
    @Log(title = "服务类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Cleaning cleaning) {
        List<Cleaning> list = cleaningService.selectCleaningList(cleaning);
        ExcelUtil<Cleaning> util = new ExcelUtil<Cleaning>(Cleaning.class);
        return util.exportExcel(list, "cleaning");
    }

    /**
     * 获取服务类型详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:cleaning:query')")
    @GetMapping(value = "/{cId}")
    public AjaxResult getInfo(@PathVariable("cId") Long cId) {
        return AjaxResult.success(cleaningService.selectCleaningById(cId));
    }

    /**
     * 新增服务类型
     */
//    @PreAuthorize("@ss.hasPermi('system:cleaning:add')")
    @Log(title = "服务类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(Cleaning cleaning, MultipartFile file) {
        cleaningService.insertCleaning(cleaning);
        try {
            if (null == file) return AjaxResult.error("文件为空");
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile + "//clean//" + cleaning.getcId());
            //具体文件
            Path path = Paths.get(profile + "//clean//" + cleaning.getcId() + "//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                file1.delete();
            } else {
                Files.write(path, bytes);//创建文件
                if (true) {
                    cleaning.setcUrl("profile/clean/" + cleaning.getcId() + "/" + file.getOriginalFilename());
                    return toAjax(cleaningService.updateCleaning(cleaning));
                }
            }
        } catch (IOException e) {

        }
        return null;
    }

    /**
     * 修改服务类型
     */
//    @PreAuthorize("@ss.hasPermi('system:cleaning:edit')")
    @Log(title = "服务类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(Cleaning cleaning, MultipartFile file) {

        if (null != file) {
            try {
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = file.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//clean//" + cleaning.getcId());
                //具体文件
                Path path = Paths.get(profile + "//clean//" + cleaning.getcId() + "//" + file.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    file1.delete();
                }
                Files.write(path, bytes);//创建文件
                cleaning.setcUrl("profile/clean/" + cleaning.getcId() + "/" + file.getOriginalFilename());

            } catch (IOException e) {
            }
            return toAjax(cleaningService.updateCleaning(cleaning));
        } else {
            return toAjax(cleaningService.updateCleaning(cleaning));
        }
    }

    /**
     * 删除服务类型
     */
//    @PreAuthorize("@ss.hasPermi('system:cleaning:remove')")
    @Log(title = "服务类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cIds}")
    public AjaxResult remove(@PathVariable Long[] cIds) {
        return toAjax(cleaningService.deleteCleaningByIds(cIds));
    }
}
